package com.adtech.core.util;

import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.ss.usermodel.CreationHelper;
import org.apache.poi.ss.usermodel.DataFormat;
import org.apache.poi.ss.usermodel.Font;
import org.apache.poi.ss.usermodel.Workbook;

/**
 * 导出工具类
 * 
 * @author Dormin
 * @since 2012-10-15
 * 
 * @Copyright (C) 2012 天津天大求实电力新技术股份有限公司 版权所有
 */
public class WorkBookHelper {
	
	private static final String DATE_FORMAT = "yyyy-MM-dd";
	private static final String NUMBER_FORMAT = "0.00";
	private static final String INTEGER_FORMAT = "0";
	private static final String TEXT_FORMAT = "text";
	private static final String STRING_TYPE = "string";
	private static final String NUMBER_TYPE = "number";
	
	private Workbook wb;	
	private CellStyle style;
    private CellStyle boldStyle;
    private CellStyle titleStyle;
	private CellStyle textStyle;
	private CellStyle dateStyle;
	private CellStyle intStyle;
	private CellStyle numberStyle;
	private CellStyle nameTitleStyle;
	private CellStyle nameStyle;
	private CellStyle nameBoldStyle;
	
	public WorkBookHelper(Workbook wb) {
		this.wb = wb;
		
		if(wb!=null) {
			Font font = ExcelUtil.createFont(wb, Font.BOLDWEIGHT_NORMAL, Font.COLOR_NORMAL);
            Font boldFont = ExcelUtil.createFont(wb, Font.BOLDWEIGHT_BOLD, Font.COLOR_NORMAL);
            
			DataFormat format  =  wb.createDataFormat();
			
			style = ExcelUtil.createThinBorderCellStyle(wb);
			style.setFont(font);
			
            titleStyle = ExcelUtil.createThinBorderCellStyle(wb);
            titleStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
            titleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
            titleStyle.setFont(boldFont);

            boldStyle = ExcelUtil.createThinBorderCellStyle(wb);
            boldStyle.setFont(boldFont);
            
            nameBoldStyle = ExcelUtil.createThinBorderCellStyle(wb);
            nameBoldStyle.setFont(boldFont);
            nameBoldStyle.setAlignment(CellStyle.ALIGN_LEFT);
			
			textStyle = ExcelUtil.createThinBorderCellStyle(wb);
			textStyle.setFont(font);
			textStyle.setDataFormat(format.getFormat(TEXT_FORMAT));
			
			dateStyle = ExcelUtil.createThinBorderCellStyle(wb);
			dateStyle.setFont(font);
			CreationHelper createHelper = wb.getCreationHelper();
			dateStyle.setDataFormat(createHelper.createDataFormat().getFormat(DATE_FORMAT));
			
			intStyle = ExcelUtil.createThinBorderCellStyle(wb);
			intStyle.setFont(font);
			intStyle.setDataFormat(format.getFormat(INTEGER_FORMAT));
			
			numberStyle = ExcelUtil.createThinBorderCellStyle(wb);
			numberStyle.setFont(font);
			numberStyle.setDataFormat(format.getFormat(NUMBER_FORMAT));
			
			nameTitleStyle = ExcelUtil.createThinBorderCellStyle(wb);
			nameTitleStyle.setFillForegroundColor(HSSFColor.GREY_25_PERCENT.index);
			nameTitleStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
			nameTitleStyle.setFont(boldFont);
			nameTitleStyle.setAlignment(CellStyle.ALIGN_LEFT);
            
            nameStyle = ExcelUtil.createThinBorderCellStyle(wb);
            nameStyle.setFont(font);
            nameStyle.setDataFormat(format.getFormat(TEXT_FORMAT));
            nameStyle.setAlignment(CellStyle.ALIGN_LEFT);
			
		}
	}
	public CellStyle getTextStyle() {
		return textStyle;
	}

	public CellStyle getDateStyle() {
		return dateStyle;
	}

	public CellStyle getIntStyle() {
		return intStyle;
	}

	public CellStyle getNumberStyle() {
		return numberStyle;
	}
	
	public Workbook getWorkbook() {
		return wb;
	}
	public CellStyle getStyle() {
		return style;
	}
    public CellStyle getBoldStyle() {
        return boldStyle;
    }
    public CellStyle getTitleStyle() {
        return titleStyle;
    }
	
	public CellStyle getNameTitleStyle() {
		return nameTitleStyle;
	}
	public void setNameTitleStyle(CellStyle nameTitleStyle) {
		this.nameTitleStyle = nameTitleStyle;
	}
	public CellStyle getNameStyle() {
		return nameStyle;
	}
	public void setNameStyle(CellStyle nameStyle) {
		this.nameStyle = nameStyle;
	}
	
	public CellStyle getNameBoldStyle() {
		return nameBoldStyle;
	}
	public void setNameBoldStyle(CellStyle nameBoldStyle) {
		this.nameBoldStyle = nameBoldStyle;
	}
	public static CellStyle getErrorStyle(Workbook wb) {
		Font font = ExcelUtil.createFont(wb, Font.BOLDWEIGHT_BOLD, Font.COLOR_NORMAL);
		CellStyle cellStyle = ExcelUtil.createThinBorderCellStyle(wb);
		cellStyle.setFillForegroundColor(HSSFColor.RED.index);
		cellStyle.setFillPattern(CellStyle.SOLID_FOREGROUND);
		cellStyle.setFont(font);
		return cellStyle;
	}
	

}
